package com.atguigu.yygh.hosp.service.impl;

import com.atguigu.yygh.client.cmn.DictFeignClient;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;//用来操作MongDb数据库
    @Autowired//注入远程调用接口
    private DictFeignClient dictFeignClient;

    @Override
    public void saveHosp(Hospital hospital) {
        if (hospital==null){
            //抛异常
           throw new YyghException(20001,"数据出错了");
        }
        //根据医院编号 查数据库中是否有医院相关信息
      Hospital targerHospital =  hospitalRepository.findHospByHoscode(hospital.getHoscode());
       if (targerHospital==null){//添加
           //添加
           hospital.setCreateTime(new Date());
           hospital.setUpdateTime(new Date());
           hospital.setIsDeleted(0);
           hospital.setStatus(0);
           hospitalRepository.save(hospital);
       }else {//修改
           hospital.setId(targerHospital.getId());
           hospital.setCreateTime(targerHospital.getCreateTime());
           hospital.setUpdateTime(new Date());
           hospital.setStatus(targerHospital.getStatus());
           hospitalRepository.save(hospital);
       }

    }
 //查询医院
    @Override
    public Hospital getHospByHoscode(String hoscode) {

        return hospitalRepository.findHospByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //0为第一页
        Pageable pageable = PageRequest.of(page-1, limit, sort);

        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> pageModel = hospitalRepository.findAll(example, pageable);
        List<Hospital> list = pageModel.getContent();
        //遍历数据list得到每个Hospital对象
        list.stream().forEach(item->{
            this.packHospital(item);
        });
        //远程调用 把每个Hospital对象编号对应名称取到 封装到map

        return pageModel;
    }
    //更新医院上线状态
    @Override
    public void updateHospStatus(String id, Integer status) {
        if (status.intValue()==0||status.intValue()==1){
            //根据id查询
            Hospital hospital = hospitalRepository.findById(id).get();
            //设置状态 修改时间
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            //调用修改
            hospitalRepository.save(hospital);
        }
    }
    //查询医院详情
    @Override
    public Map<String, Object> getHospById(String id) {
        Hospital hospital =  hospitalRepository.findById(id).get();
        Map<String,Object> map =new HashMap<>();
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }
    //获取医院名称
    @Override
    public String getHosNameByHoscode(String hoscode) {
        return hospitalRepository.findHospByHoscode(hoscode).getHosname();
    }

    //条件查询 获取医院列表
    @Override
    public List<Hospital> selectHospList(HospitalQueryVo hospitalQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        Example<Hospital> example = Example.of(hospital, matcher);
        List<Hospital> list = hospitalRepository.findAll(example, sort);
        list.stream().forEach(item->{
            this.packHospital(item);
        });
        return list;
    }
    //模糊查询
    @Override
    public List<Hospital> getHospName(String hosname) {
        return hospitalRepository.findHospByHosnameLike(hosname);

    }
    //根据医院编号查询医院详情
    @Override
    public Map<String, Object> getHosp(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.packHospital(this.getHospByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }


    //根据编号获取名称  封装到map
    private Hospital packHospital(Hospital hospital) {
        //封装省编号
        String provinceCode = hospital.getProvinceCode();//获取省的编号
        String provinceName = dictFeignClient.getName(provinceCode);//获取省的名称
        //封装市编号
        String cityName = dictFeignClient.getName(hospital.getCityCode());
        //封装区编号
        String districtName = dictFeignClient.getName(hospital.getDistrictCode());
        //封装医院编号
        String hostypeName = dictFeignClient.getName("Hostype", hospital.getHostype());
        //把所有名称封装进map
        Map<String, Object> map = hospital.getParam();
        map.put("hostypeName",hostypeName);
        map.put("fullAdress",provinceName+cityName+hostypeName+hospital.getAddress());
        return hospital;
    }
}
